package com.mrdeer.web.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrdeer.common.entity.beans.PageEntity;
import com.mrdeer.common.entity.dto.DepartmentDTO;
import com.mrdeer.common.entity.sys.Department;
import com.mrdeer.common.exception.BusinessException;
import com.mrdeer.security.SecurityUtil;
import com.mrdeer.web.mapper.DepartmentMapper;
import com.mrdeer.web.service.sys.SysDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class SysDepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements SysDepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private SecurityUtil securityUtil;

    @Override
    public Department add(Department department) {
        Department select = getById(department.getId());
        if (null != select) {
            throw new BusinessException("The department already exists in the database");
        }
        if (departmentMapper.insert(department) > 0) {
            return departmentMapper.selectById(department.getId());
        }
        throw new BusinessException("the department add to the database");
    }

    @Override
    public boolean delete(String id) {
        Department d = getById(id);
        if (null == d) {
            throw new BusinessException("the department does not exist in the database");
        }
        return departmentMapper.deleteById(id) > 0;
    }

    @Override
    public Department update(Department department) {
        if (departmentMapper.updateById(department) > 0) {
            Department select = departmentMapper.selectById(department.getId());
            return select;
        }
        throw new BusinessException("update department failed");
    }

    @Override
    public Department getById(String id) {
        LambdaQueryWrapper<Department> queryWrapper = Wrappers.<Department>query().lambda();
        queryWrapper.eq(Department::getId, id);
        return departmentMapper.selectOne(queryWrapper);
    }

    @Override
    public Page<Department> list(DepartmentDTO dto) {
        if (null == dto.getPage()) {
            dto.setPage(new PageEntity());
        }
        return departmentMapper.selectPage(dto.getPage().convert(), fuzzyQuery(dto));
    }

    @Override
    public LambdaQueryWrapper<Department> fuzzyQuery(DepartmentDTO dto) {
        LambdaQueryWrapper<Department> queryWrapper = Wrappers.<Department>query().lambda();
        if (null != dto) {
            if (StringUtils.isNotBlank(dto.getTitle())) {
                queryWrapper.like(Department::getTitle, dto.getTitle());
            }
            if (dto.getIds() != null) {
                queryWrapper.in(Department::getId, dto.getIds());
            }
            if (dto.getParentId() != null) {
                queryWrapper.eq(Department::getParentId, dto.getParentId());
            }
        }
        return queryWrapper;
    }

    @Override
    public List<Department> findByParentIdOrderBySortOrder(String parentId, Boolean openDataFilter) {
        DepartmentDTO dto = new DepartmentDTO();
        dto.setParentId(parentId);
        return departmentMapper.selectList(fuzzyQuery(dto));
    }

    @Override
    public List<Department> findByParentIdAndStatusOrderBySortOrder(String parentId, Integer status) {
        DepartmentDTO dto = new DepartmentDTO();
        dto.setParentId(parentId);
        dto.setStatus(status);
        return departmentMapper.selectList(fuzzyQuery(dto));
    }

    @Override
    public List<Department> findByTitleLikeOrderBySortOrder(String title, Boolean openDataFilter) {
        //数据权限
        List<String> depIds = securityUtil.getDepartmentIds();
        DepartmentDTO dto = new DepartmentDTO();
        dto.setTitle(title);
        dto.setIds(depIds);
        return departmentMapper.selectList(fuzzyQuery(dto));
    }
}
